//
//  HNTeacgerEvaluateDetailController.m
//  BestLearning
//
//  Created by 星道 on 17/6/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNTeacgerEvaluateDetailController.h"
#import "HNTeacgerEvaluateView.h"
#import "HNTeacgerEvaluateDetailModel.h"
#import "HNAudioView.h"
#import "HNAudioTapGestureRecognizer.h"
#import "EMCDDeviceManager.h"
#import "ReceiveFlowerView.h"
#import "ZYMiyoshiBankController.h"
#define LLScreenW [UIScreen mainScreen].bounds.size.width
#define LLScreenH [UIScreen mainScreen].bounds.size.height
@interface HNTeacgerEvaluateDetailController ()
{
    //布局用
    IBOutlet UIView *_ScrollView;
    IBOutlet NSLayoutConstraint *_ScrollViewHeight;
    IBOutlet NSLayoutConstraint *_TopViewConstraint;


    IBOutlet UILabel *_XueShengAccount;
    IBOutlet UILabel *_Type;
    IBOutlet UILabel *_TypeTitle;

    IBOutlet UIView *_ImgFView;
    IBOutlet NSLayoutConstraint *_ImgFViewConstraint;
    IBOutlet UIImageView *_ImgF_image;

    IBOutlet UILabel *_commonPro;
    IBOutlet UIImageView *_CommonImage;

    IBOutlet UILabel *_PersonPro;
    IBOutlet UIImageView *_PersonImage;
    HNTeacgerEvaluateDetailModel *_Model;

    NSMutableArray *_ImageArr;
}
@property (nonatomic, strong) HNAudioView *AudioView;
@end

@implementation HNTeacgerEvaluateDetailController

- (void)viewDidLoad
{
    [super viewDidLoad];

    
    self.title = @"课程评论详情";

    [self RefreshData];




    [self setNetWorkTow];

}


- (void)setNetWorkTow{
   // http://192.168.0.113:8080/StarShine/ws/aflatunLesson.do?action=getCommentDetailById2&id=5&isRead=1
   
    
    NSString *url = [NSString stringWithFormat:@"aflatunLesson.do?action=saveCommentViewRecord&lessonId=%@&enrollId=%@&commentId=%@",self.lessonId,self.enrollId,self.ID];
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
    } Fail:^(NSError *Error) {
        
    }];
    
   
    
    
    
}







- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[EMCDDeviceManager sharedInstance] stopPlaying];
}



#pragma mark - 网络请求
- (void)RefreshData
{
    [MBProgressHUD showHUD];
    
    
    NSString *UrlStr = [NSString stringWithFormat:@"aflatunLesson.do?action=getCommentDetailById2&id=%@&isRead=1",self.ID];

    NSLog(@"%@ %@",LLBaseURLs(UrlStr),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    [NetManager Get:LLBaseURLs(UrlStr) Auth:YES Params:nil Success:^(id Obj) {
        [MBProgressHUD hideHUD];
        if ([Obj[@"code"] isEqualToNumber:@1000])
        {
            _Model = [[HNTeacgerEvaluateDetailModel alloc] init_HNObjectForKeys:Obj[@"t"]];
            [self GetModel];
            
            // 奖励红花的弹窗
            NSString *str = Obj[@"t"][@"param"];
            if (str.length > 0 && ![str isKindOfClass:[NSNull class]]) {
                
                [ReceiveFlowerView receiveFlowerWithMsg:str Complete:nil];
            }
        }
        else
        {
            [MBProgressHUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"链接超时"];
    }];
    
    
    
}

/**
 获取amr文件
 */
- (void)RefreshDataForAmr:(NSString *)UrlStr Complete:(void(^)(NSString *FullPath))Complete
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"AMR"];

    
   
    
    
    [NetManager DownLoadFileWithUrlStr:UrlStr FileName:[UrlStr lastPathComponent] Path:path Progress:^(CGFloat Index) {


    } Complete:^(NSString *FullPath, NSError *Err) {

        [MBProgressHUD hideHUD];
        if (Err)
        {
            [MBProgressHUD showError:@"音频文件播放失败"];
        }
        else
        {
            Complete (FullPath);
        }
    }];
}




#pragma mark - 屏幕赋值
- (void)GetModel
{
    _XueShengAccount.text = _Model.username;
    if ([_Model.subjectId integerValue] <= 0) {

        _TypeTitle.text = @"课程名称";
        _Type.text      = self.SubjectTitle;
        
    } else {

        _Type.text            = _Model.subjectId;
    }
    if (_Model.ImgF_Arr.count > 0)
    {
        _ImageArr = [NSMutableArray array];
        for (int i=0; i<_Model.ImgF_Arr.count; i++)
        {
            UIImageView *ImageView = [[UIImageView alloc] init];
            ImageView.frame = CGRectMake((78*i)+8, 0, _ImgF_image.width, _ImgF_image.width);
            ImageView.center = CGPointMake(ImageView.centerX, _ImgF_image.centerY);
            [_ImgFView addSubview:ImageView];
            [_ImageArr addObject:ImageView];

            /** 增加播放手势 */

            ImageView.userInteractionEnabled = YES;


            BOOL IsPicture = [self JudgImgAndAudio:_Model.ImgF_Arr[i]];
            if (IsPicture)
            {
                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                [ImageView sd_setImageWithURL:[NSURL URLWithString:_Model.ImgF_Arr[i]] placeholderImage:[UIImage imageNamed:@"icon_add_defaulttt"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    Tap.Img = ImageView.image;
                }];
                [ImageView addGestureRecognizer:Tap];
            }
            else
            {
                ImageView.image = [UIImage imageNamed:@"btn_mp3"];
                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                [ImageView addGestureRecognizer:Tap];
                Tap.UrlStr = _Model.ImgF_Arr[i];
            }

            if (i == _Model.ImgF_Arr.count-1)
            {
                _ImgFViewConstraint.constant = ImageView.frame.size.width+ImageView.frame.origin.y+8;
                _ImgF_image.hidden = YES;
            }
        }
    }

    _commonPro.text = _Model.commonProblem;
    _PersonPro.text = _Model.personalProblem;

    //判断地址是否为图片还是音频
    //共性问题图片
    if ([self JudgImgAndAudio:_Model.commonF])
    {
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
        [_CommonImage sd_setImageWithURL:[NSURL URLWithString:_Model.commonF] placeholderImage:[UIImage imageNamed:@"icon_add_defaulttt"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            Tap.Img = _CommonImage.image;
        }];
        [_CommonImage addGestureRecognizer:Tap];
        _CommonImage.userInteractionEnabled = YES;
    }
    else
    {
        _CommonImage.image = [UIImage imageNamed:@"btn_mp3"];
        /** 增加播放手势 */
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
        Tap.UrlStr = _Model.commonF;
        [_CommonImage addGestureRecognizer:Tap];
        _CommonImage.userInteractionEnabled = YES;
    }

    //个性问题图片
    if ([self JudgImgAndAudio:_Model.wmrPath])
    {
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
        [_PersonImage sd_setImageWithURL:[NSURL URLWithString:_Model.wmrPath] placeholderImage:[UIImage imageNamed:@"icon_add_defaulttt"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            Tap.Img = _PersonImage.image;
        }];
        [_PersonImage addGestureRecognizer:Tap];
        _PersonImage.userInteractionEnabled = YES;
    }
    else
    {
        _PersonImage.image = [UIImage imageNamed:@"btn_mp3"];
        /** 增加播放手势 */
        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
        Tap.UrlStr = _Model.wmrPath;
        [_PersonImage addGestureRecognizer:Tap];
        _PersonImage.userInteractionEnabled = YES;
    }


    [self CreatUI];
}



/** YES是图片,NO是音频 */
- (BOOL)JudgImgAndAudio:(NSString *)Str
{
    if ([Str isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else
    {
        if ([HNTeacgerEvaluateDetailModel JudgeStrIsImagOrAudio:Str])
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
}









#pragma mark - UI
- (void)CreatUI
{


    CGFloat Height1 = 40 * _Model.HaoChengJi_Arr.count;
    HNTeacgerEvaluateView *xx1 = [[HNTeacgerEvaluateView alloc] init_Title:@"好习惯" ContentArr:_Model.HaoChengJi_Arr Y:_TopViewConstraint.constant W:LLScreenW-32];
    [_ScrollView addSubview:xx1];

    CGFloat Height2 = 40 * _Model.HaoXiGuan_Arr.count;
    HNTeacgerEvaluateView *xx2 = [[HNTeacgerEvaluateView alloc] init_Title:@"好成绩" ContentArr:_Model.HaoXiGuan_Arr Y:_TopViewConstraint.constant+Height1 W:LLScreenW-32];
    [_ScrollView addSubview:xx2];

    CGFloat Height3 = 40 * _Model.HaoSuYang_Arr.count;
    HNTeacgerEvaluateView *xx3 = [[HNTeacgerEvaluateView alloc] init_Title:@"好素养" ContentArr:_Model.HaoSuYang_Arr Y:_TopViewConstraint.constant+Height1+Height2 W:LLScreenW-32];
    [_ScrollView addSubview:xx3];

    _ScrollViewHeight.constant = _TopViewConstraint.constant+Height1+Height2+Height3;

    _ScrollViewHeight.constant += 40;
    UILabel *Label = [UILabel new];
    Label.frame = CGRectMake(16, _ScrollViewHeight.constant-40, _ScrollView.frame.size.width, 40);
    NSInteger Source = [HNTeacgerEvaluateView SourceCount:_Model.HaoChengJi_Arr]+[HNTeacgerEvaluateView SourceCount:_Model.HaoXiGuan_Arr]+[HNTeacgerEvaluateView SourceCount:_Model.HaoSuYang_Arr];
    Label.text = [NSString stringWithFormat:@"总分            %ld分",(long)Source];
    [_ScrollView addSubview:Label];
}










#pragma mark - 图片全屏
/** 图片全屏手势 */
- (void)ImgAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self ImgControllerWithImg:Tap.Img];
}
/** 图片全屏操作 */
- (void)ImgControllerWithImg:(UIImage *)Img
{
    
    
    
    [self.AudioView OpenWithImg:Img];
}


#pragma mark - 音频播放
/** 播放手势 */
- (void)PlayerAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self PlayerWithUrlStr:Tap.UrlStr];
}
/** 音频播放 */
- (void)PlayerWithUrlStr:(NSString *)UrlStr
{
    [self RefreshDataForAmr:UrlStr Complete:^(NSString *FullPath) {

        FullPath = [FullPath substringFromIndex:7];
        [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:FullPath completion:^(NSError *error) {
            NSLog(@"播放完成");
        }];
    }];
}


#pragma mark - Setter
- (HNAudioView *)AudioView
{
    if (!_AudioView)
    {
        _AudioView = [[HNAudioView alloc] init];
    }
    return _AudioView;
}
@end
